///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_monster_db_logic.cpp
// Created: 2013-8-9
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////

#include "hy_monster_logic.h"
#include "hy_monster_db_logic.h"
#include "hy_battle_system/hy_server_monster_data.h"
#include "hy_battle_system/hy_server_monster_group_data.h"

#include "hy_db/hy_db_logic.h"
#include "hy_db/hy_db_const.h"


hy_monster_db_logic* hy_monster_db_logic_instance = NULL;
hy_monster_db_logic::hy_monster_db_logic()
{

}

hy_monster_db_logic::~hy_monster_db_logic()
{

}

hy_monster_db_logic* hy_monster_db_logic::instance()
{
	if( hy_monster_db_logic_instance == NULL )
		hy_monster_db_logic_instance = new hy_monster_db_logic;
	return hy_monster_db_logic_instance;
}

void hy_monster_db_logic::purge_instance()
{
	DEL_POINTER( hy_monster_db_logic_instance );
}

bool hy_monster_db_logic::reload_monster_template_data()
{
	hy_server_monster_data_map& hmap = hy_server_monster_mgr::instance()->get_server_monster_data_map();
	hy_server_monster_data_map::iterator it;
	for( it = hmap.begin(); it != hmap.end(); it++ )
	{
		DEL_POINTER( it->second );
	}
	hy_server_monster_mgr::instance()->get_server_monster_data_map().clear();

	return load_monster_template_data();
}

bool hy_monster_db_logic::load_monster_sequence_data()
{
	Json::Value rjv = load_jsonfile_val("game_data/sequence.json");
	Json::Value return_val = rjv["monster_sequence"];

	for( Json::Value::iterator it = return_val.begin(); 
		it != return_val.end(); it++ )
	{
		int key = ::atoi( it.key().asString().c_str() );

		Json::Value& v = *it;

		int_vec iv;
		for( unsigned int i = 0u; i < v.size(); i++ )
		{
			iv.push_back( v[i].asInt() );
		}
		hy_monster_logic::instance()->m_monster_sequence.insert( std::make_pair( key, iv ) );
	}
	return true;
}
bool hy_monster_db_logic::load_monster_template_data()
{
	/*
	std::string table = HY_DB_AND( HY_DB, HY_SERVER_TABLE_MONSTER_TEMPLATE );

	mongo::BSONObjBuilder con;
	Json::Value return_val; 
	if( !hy_db_logic::instance(DS)->load_collection_data( con, table.c_str(), return_val ) )
		return false;
	*/

	load_monster_sequence_data();

	Json::Value rjv = load_jsonfile_val("game_data/monster.json");
	Json::Value return_val = rjv["monster_list"];

	for( unsigned int i = 0u; i < return_val.size(); i++ )
	{
		Json::Value& v = return_val[i];

		int id		= v["id"].asInt();
		std::string name = v["name"].asString();

		hy_server_monster_data* d = new hy_server_monster_data;
		d->init();
		d->set_id( id );
		d->m_attack = v["attack"].asInt();
		d->m_hp = v["hp"].asInt();
		d->m_agile = v["agile"].asInt();
		
		d->m_skill_set.clear();
		for( size_t i = 0u; i < v["default_skill_id"].size(); i++ )
		{
			d->m_skill_set.insert( v["default_skill_id"][i].asInt() );
		}

		d->set_value( v );

		hy_server_monster_mgr::instance()->add_server_monster_data( d, true );
	}	
	return true;
}
///////////////////////////////////////////////////////////////////////////////
//hy_monster_db_logic::load_monster_group_template_data
///////////////////////////////////////////////////////////////////////////////
bool hy_monster_db_logic::load_monster_group_template_data()
{
	//std::string table = HY_DB_AND( HY_DB, HY_SERVER_TABLE_MONSTER_GROUP_TEMPLATE );
	//mongo::BSONObjBuilder con;	
	//if( !hy_db_logic::instance(DS)->load_collection_data( con, table.c_str(), return_val ) )
	//	return false;

	Json::Value rjv = load_jsonfile_val("game_data/monster_group.json");
	Json::Value return_val = rjv["monster_group_list"];

	for( unsigned int i = 0u; i < return_val.size(); i++ )
	{
		Json::Value& v = return_val[i];

		int id		= v["id"].asInt();
		std::string name = v["name"].asString();

		hy_server_monster_group_data* d = new hy_server_monster_group_data;
		d->init();
		d->set_id( id );
		d->set_value( v );
		d->parse_data();

		hy_server_monster_group_mgr::instance()->add_server_monster_group_data( d );
	}	

	return true;
}